package com.fireXXX.job.analysis.crawler.boss;

import com.fireXXX.job.analysis.crawler.boss.constant.CityTypeEnum;
import com.fireXXX.job.analysis.crawler.boss.constant.EmployeeNumberTypeEnum;
import com.fireXXX.job.analysis.crawler.boss.constant.IndustryTypeEnum;
import com.fireXXX.job.analysis.crawler.boss.constant.PublicStepTypeEnum;
import com.fireXXX.job.analysis.crawler.boss.data.OriginCompany;
import com.fireXXX.job.analysis.crawler.boss.data.OriginJobItem;
import com.fireXXX.job.analysis.crawler.common.Page;
import com.fireXXX.job.analysis.crawler.error.TaskTypeEndCrawlerException;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class BossCompanyParser {

    private OriginCompany parseOne(CityTypeEnum city, IndustryTypeEnum industry, PublicStepTypeEnum publicStep, EmployeeNumberTypeEnum employeeNumber,
                                   Element element) {
        //招聘概要信息
        Element conpany = element.getElementsByClass("conpany-text").get(0);
        String name = conpany.getElementsByTag("h4").get(0).text();

        OriginCompany originCompany = new OriginCompany();
        originCompany.setCity(city.getText());
        originCompany.setName(name);
        originCompany.setPublicStep(publicStep.getText());
        originCompany.setIndustry(industry.getText());
        originCompany.setEmployeeNumber(employeeNumber.getText());

        return originCompany;
    }

    public List<OriginCompany> parse(CityTypeEnum city, IndustryTypeEnum industry, PublicStepTypeEnum publicStep, EmployeeNumberTypeEnum employeeNumber, Page page,
                                     String content) {
        List<OriginCompany> originCompanies = new ArrayList<>();

        Document document = Jsoup.parseBodyFragment(content);
        //获取body
        Element body = document.body();
        Elements companyList = null;
        try {
            companyList = body.getElementById("main").getElementsByClass("company-list").get(0).getElementsByTag("ul").get(0).children();
        } catch (IndexOutOfBoundsException exception) {
            throw new TaskTypeEndCrawlerException();
        }

        if (companyList == null || companyList.size() == 0) {
            throw new TaskTypeEndCrawlerException();
        }

        for (int i = 0; i < companyList.size(); i++) {
            Element company = companyList.get(i);

            OriginCompany item = null;
            try {
                item = parseOne(city, industry, publicStep, employeeNumber, company);
            } catch (Exception e) {
                log.error("错误任务:" + company.html());
                e.printStackTrace();
            }

            if (item != null) {
                originCompanies.add(item);
            }
        }

        return originCompanies;
    }

}
